import { Commit, Getter, GetterTree, Action, ActionTree, MutationTree } from 'vuex'
import axios from 'axios';
import {InfoGet } from '@/store/common/room'
import API from '@/api'



export interface State {
    InfoGetList: InfoGet[],
    InfoGetAllList:InfoGet[],
}

const initState: State = {
    InfoGetList:[],
    InfoGetAllList:[],
}

const infoGet: Getter<State, any> = (state: State) => {
    return state.InfoGetList
}

const infoGetAll: Getter<State, any> = (state: State) => {
    return state.InfoGetAllList
}



const infoGetAction: Action<State, any> = 
(context: { commit: Commit }) => {
    return new Promise((resolve, reject) => {
        axios({
            params:{
                id:"13"
            },
            url: API.RoomInfo.Get
        }).then((response) => {
            // console.log(response.data.result)
            // const payload: PatientInfo = response && response.data;
            context.commit('infoGetM', response.data.result);
            resolve(response.data.result)
        }, (error) => {
            console.log(error);
            context.commit('profileError');
            reject(error)
        });
    });
}

const infoGetAllAction: Action<State, any> = 
(context: { commit: Commit }) => {
    return new Promise((resolve, reject) => {
        axios({
            url: API.RoomInfo.GetAll
        }).then((response) => {
            console.log(response.data.result)
            // const payload: PatientInfo = response && response.data;
            // response.data.result.items.map((m: any) => {
            //     m.operations = JSON.parse(m.operations)
            //     let names : string[] = [];
            //     m.operations.forEach((n: any) => {
            //         names.push(n.name)
            //     });
            //     m.operationNames = names;
            // })
            context.commit('infoGetAllM', response.data.result.items);
            resolve(response.data.result.items)
        }, (error) => {
            console.log(error);
            context.commit('profileError');
            reject(error)
        });
    });
}


const mutations: MutationTree<State> = {
   
    infoGetM(state, list: InfoGet[]) {
        state.InfoGetList = list
    },
    infoGetAllM(state, list: InfoGet[]) {
        state.InfoGetAllList = list
    }
    
};

const getters: GetterTree<State, any> = {
  
    infoGet,
    infoGetAll,
   
};

const actions: ActionTree<State, any> = {
  
    infoGetAction,
    infoGetAllAction,
   
}

export default {
    namespaced: true,
    state: initState,
    getters,
    actions,
    mutations,
}